<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Widgets for displaying the photo news stream.
 * Includes a list widget and its specific item widget.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from the NewsStream and NewsStreamItem.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_newsstream.php';

require_once INSTALLDIR . '/classes/User.php';
require_once INSTALLDIR . '/classes/PhotoSet.php';
require_once INSTALLDIR . '/classes/Photo.php';

/**
 * Widget for displaying a list of photo news.
 */
class PhotoNewsStream extends NewsStream
{
	/**
	 * Overrided.
	 * Starts a news stream(list).
	 */
	function showStart()
	{
		/*
		$this->out->elementStart('dl', array('class' => 'cls_dl_newsstream'));
		
		// The News title.
		$this->out->element('dt', null, '图片新闻! @新鲜事');
		*/
		$this->out->element('div');
		$this->out->element('h3', null, '图片新闻');
		$this->out->elementStart('ol', array('class' => 'cls_ol_list'));
		
		$this->out->element('hr');
	}
	
	/**
	 * Ends a list.
	 */
	function showEnd()
	{
		$this->out->elementEnd('ol');
	}
	
	/**
	 * Overrided.
	 * Returns a new stream item for the current sub news.
	 */
	function newListItem(&$subnews)
	{
		return new PhotoNewsStreamItem($subnews, $this->out);
	}
}

/**
 * Widget for displaying a single photo news item with some buttons.
 */
class PhotoNewsStreamItem extends NewsStreamItem
{
	/**
	 * The poster user.
	 */
	var $postuser;
	
	/**
	 * Url of the photoset.
	 */
	var $photoseturl;
	
	/**
	 * Overrided.
	 */
	function prepareData()
	{
		$this->postuser = '社联会';
		$usertype = User::getUserTypeById($this->subnews->user_id);
		if (USER_TYPE_ASSOCIATION == $usertype) {
			$this->postuser = User::getTitleByUserid($this->subnews->user_id);
		}
		
		$this->photoseturl = slh_action2url('photoset', array('gphotoset_id' => $this->subnews->photoset_id));
	}
	
	/**
	 * Overrided.
	 */
	function showStart()
	{
		$this->out->elementStart('li', array('class' => 'cls_div_entry'));
	}
	
	function showEnd()
	{
		$this->out->elementEnd('li');
		
		$this->out->element('hr');
	}
	
	/**
	 * Overrided.
	 */
	function showDataInfo()
	{
		$this->out->elementStart('div', 'cls_div_photosettitle');
		$this->out->raw('<span class="cls_span_photosettitle">' .
						'<a target="_blank" href="' .
						slh_action2url('photoset', array('gphotoset_id' => $this->subnews->photoset_id)) .
						'">' . 
						$this->postuser . '</a>' .
						' 上传了新的图片到 ' . 
						'<a target="_blank" href="' .
						slh_action2url('photoset', array('gphotoset_id' => $this->subnews->photoset_id)) .
						'">' . 
						$this->subnews->getPhotosetTitle() . 
						'</a>' . 
						'</span>');
		$this->out->elementEnd('div');
	
		$this->out->elementStart('div', array('class' => 'cls_div_photothumbnails'));
		$this->out->elementStart('ul');
		foreach (array($this->subnews->photo_ida, 
					   $this->subnews->photo_idb, 
					   $this->subnews->photo_idc) as $photoid)
	    {
	   		$src = Photo::getThumbnailUrlById($photoid);
		    if ( !empty($src)) {
				$this->out->elementStart('li');
				$this->out->elementStart('div', array('class' => 'cls_div_photothumbnail_item'));
				$this->out->elementStart('a', array('href' => $this->photoseturl));
				$this->out->element('img', array('src' => $src));
				$this->out->elementEnd('a');
				$this->out->elementEnd('div');
				$this->out->elementEnd('li');
			}
	    }
	    $this->out->elementEnd('ul');
	    $this->out->elementEnd('div');
	    
	    $this->out->raw('<span class="cls_span_author">' .
						'来自: ' . 
						User::getTitleByUserid($this->subnews->user_id) .
						'</span>');
			
		$this->out->raw('<span class="cls_span_created">' .
						slh_date_string($this->subnews->created) .
						'</span>');
	}
	
	/**
	 * Overrided.
	 */
	function showOptions()
	{
		$this->out->elementStart('div', 'cls_div_newsoptions');
		
// TODO: Check if has rights to delete or more options.
		$this->out->elementStart('ul', array('class' => 'cls_ul_inline '));
			if (true) {
			$this->out->elementStart('li');
			$this->out->element('a', array('href' => slh_action2url('deletephotonews',
								  	   							  	array('gnews_id' => $this->subnews->news_id)),
										   'title'=> '把这条相册新鲜事删掉哈!'),
								'删除');
			$this->out->elementEnd('li');
			}
			
			if (false) {
			$this->out->elementStart('li');
			$this->out->elementEnd('li');
			}
		$this->out->elementEnd('ul');
		
		$this->out->elementEnd('div');
	}
}

?>